﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using System.IO;
using System.Drawing;
using ELTE.TFEG.ImageTransform;
using ELTE.TFEG.Hough.EventHandling;
using ELTE.TFEG.ImageTransform.EventHandling;

namespace ELTE.TFEG.Hough.Wpf.Classes
{
    public class ImageFileManager
    {

        ImageProcess imageProcessor = new ImageProcess();

        public List<string> GetImageFilesInFolders(string imageFileFolder, List<string> availableExtensions, List<string> suffixesToExclude)
        {
            List<string> result = new List<string>();
            try
            {
                string[] filesInFolder = Directory.GetFiles(imageFileFolder);
                var filteredFiles = filesInFolder.Where(f =>
                    availableExtensions.Contains(f.Substring(f.LastIndexOf('.')))
                    && !suffixesToExclude.Contains(f.GetSuffix())).ToList();
                if (filteredFiles.Count() > 0)
                {
                    filteredFiles.ForEach(
                        f => result.Add(f));
                }
            }
            catch (Exception ex)
            {
            }
            return result;
        }

        public List<Bitmap> GetEdgeDetectedImages(Bitmap image)
        {
            List<Bitmap> result = new List<Bitmap>();
            try
            {
                string originalPath = image.Tag.ToString();
                //check if the image already exists
                string grayScaledImagePath = originalPath.AddSuffixToFilePath(Constants.grayScaleSuffix);
                Bitmap grayScaledImage = null;
                if (File.Exists(grayScaledImagePath))
                {
                    //if the grayscaled file exists, we simply load it.
                    grayScaledImage = new Bitmap(grayScaledImagePath);
                }
                else
                {
                    //if the grayscaled file does not exists, we will load it.
                    grayScaledImage = imageProcessor.GetGrayScaledImage(image);
                    grayScaledImage.Save(grayScaledImagePath);
                }
                if (grayScaledImage != null)
                {
                    //check if the edge detected images are existing
                    //if so, we simply load them, else generate & save them
                    string homImagePath = originalPath.AddSuffixToFilePath(Constants.homFilterSuffix);
                    string canImagePath = originalPath.AddSuffixToFilePath(Constants.canFilterSuffix);
                    string sobImagePath = originalPath.AddSuffixToFilePath(Constants.sobFilterSuffix);
                    string difImagePath = originalPath.AddSuffixToFilePath(Constants.diffFilterSuffix);
                    if (File.Exists(homImagePath) && File.Exists(canImagePath) && File.Exists(sobImagePath) && File.Exists(difImagePath))
                    {
                        result.Add(new Bitmap(homImagePath));
                        result.Add(new Bitmap(canImagePath));
                        result.Add(new Bitmap(sobImagePath));
                        result.Add(new Bitmap(difImagePath));
                    }
                    else
                    {
                        result = imageProcessor.GetEdgeDetectedImages(grayScaledImage);
                        result.ForEach(bmp => bmp.Save(originalPath.AddSuffixToFilePath(bmp.Tag.ToString())));
                    }
                }

            }
            catch (Exception ex)
            {
            }
            
            return result;
        }

        public List<Bitmap> GetHough(Bitmap edgeDetected, List<HoughTransformMode> modes, int percentage, bool shouldDistinct, bool shouldTraceDown,int radius = 0)
        {

            List<Bitmap> result = new List<Bitmap>();
            BitmapImage img = edgeDetected.GetBitmapImage();
            if (modes.Any(m => m == HoughTransformMode.HoughCircle))
            {
                result.Add(imageProcessor.GetHoughCircle(img, percentage, radius, shouldDistinct, shouldTraceDown));
            }
            if (modes.Any(m => m == HoughTransformMode.HoughLine))
            {
                result.Add(imageProcessor.GetHoughLine(img, percentage));
            }
            if (modes.Any(m => m == HoughTransformMode.HoughLineSegment))
            {
                result.Add(imageProcessor.GetHoughLineSegments(img, percentage));
            }

            return result;
        }
    }
}
